from models.global_config import WebGlobalConfig
from rest_framework_mongoengine.serializers import DocumentSerializer, serializers


class TokenSerializer(DocumentSerializer):
    class Meta:
        model = WebGlobalConfig
        fields = [
            'access_token_limit',
            'max_retry_count',
            'locked_time',
            'max_multiple_devices',
        ]

    access_token_limit = serializers.IntegerField(min_value=10, max_value=120, default=15)
    max_retry_count = serializers.IntegerField(min_value=1, default=5, max_value=5)
    locked_time = serializers.IntegerField(min_value=1, default=10, max_value=20)
    max_multiple_devices = serializers.IntegerField(min_value=1, default=3)


def get_global_config():
    obj_config = WebGlobalConfig.objects.filter().first()
    if not obj_config:
        obj_config = WebGlobalConfig().save()

    return obj_config
